/*
 * @Author: funlee
 * @Email: i@funlee.cn
 * @Date: 2018-05-06 10:38:21
 * @Description: 避免全局污染
 */

/**
* 使用命名空间
*/
var namespace1 = {
  a: function () {
    alert(1);
  },
  b: function () {
    alert(2);
  }
}
/**
 * 动态创建命名空间
 */

var MyApp = {};
MyApp.namespace = function (name) {
  var parts = name.split('.');
  var current = MyApp;
  console.log(parts)
  for (var i in parts) {
    if (!current[parts[i]]) {
      current[parts[i]] = {
        a: 123
      }
    }
    current = current[parts[i]]
  }
}

MyApp.namespace('event');
MyApp.namespace('dom.style');

console.dir(MyApp)

/**
 * 闭包封装私有变量
 */
var user = (function () {
  var _name = 'sven',
    _age = 22;

  return {
    getUserInfo: function () {
      return _name + '-' + _age
    }
  }
})()

console.log(user.getUserInfo())



